// @ts-ignore
import {defineStore} from "pinia";
import {UserState} from "/@/types/pinia";
import {Session} from "/@/utils/StorageTools";
import {getUserInfo, loginIn, logOut, registerAccount} from "/@/api/hz-system-api";
import {useMessage} from "/@/hooks/message";


export const useUserConfig = defineStore('user', {
    state: () : UserState => ({
        userInfo: {
            id: -1,
            username: null,
            avatar: '',
            token: null,
            date_joined: '',
            last_login: '',
            email: '',
            is_active: false,
            is_staff: false,
            is_superuser: false,
            groups: [],
            conversation_id: '407ac80c-8932-42e9-9d25-149ea85da447'
        }
    }),
    actions: {
        setUserInfo(data:any){
          this.userInfo = data
        },

        // 登录
        async login(data: { username: string, password: string }) {
            const loginResponse = await loginIn(data);
            if(loginResponse.code != 200){
                useMessage().error(loginResponse.msg);
                return;
            }
            Session.set('token', loginResponse.data.token);
            useMessage().success('登录成功');
            const userInfo = await getUserInfo();
            this.userInfo = userInfo.data
            this.userInfo.token = loginResponse.data.token;
            useMessage().success('获取用户信息成功');


        },
        // 注册
        async register(data: { username: string, password: string ,password2:string }) {
            if(data.username.length < 6){
                useMessage().error('用户名长度不能小于6位');
                throw new Error('用户名长度不能小于6位');
            }
            if(data.password.length < 6){
                useMessage().error('密码长度不能小于6位');
                throw new Error('密码长度不能小于6位');
            }
            if (data.password !== data.password2) {
                useMessage().error('两次密码不一致');
                throw new Error('两次密码不一致');
            }
            const registerResponse = await registerAccount(data);
            if(registerResponse.code !== 200){
                useMessage().error(registerResponse.msg);
                return;
            }
            useMessage().success('注册成功');
        },
        // 退出登录
        async logout() {
            await logOut();
            this.userInfo = {
                id: -1,
                username: null,
                avatar: '',
                token: null,
                date_joined: '',
                last_login: '',
                email: '',
                is_active: false,
                is_staff: false,
                is_superuser: false,
                groups: [],
            };
        }
    },
    // 持久化
    persist: {
        enabled: true,
        strategies: [
            {
                key: 'userInfo', // 存储在localStorage的key值
                paths: ['userInfo'], // 需要持久化的数据
                storage: window.localStorage, // 指定持久化存储的对象
            },
        ],
    }
})
